package com.whut.whw.goodsdetail;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.whut.whw.R;
import com.whut.whw.util.ConvertUtils;

import org.w3c.dom.Text;

import java.util.List;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/16
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class CommentRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<CommentEntity> datas;
    Context context;
    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    // 头布局
    private View mHeaderView;
    // 尾布局
    private View mFooterView;

    public CommentRecycleAdapter(Context context, List<CommentEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    public View getHeaderView() {
        return mHeaderView;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null){
            return TYPE_NORMAL;
        }
        if (position == 0){
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        if (position == getItemCount()-1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new ListHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new ListHolder(mFooterView);
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new ListHolder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        // 只有在普通布局时才绑定，header和佛呕吐而不进行绑定
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder instanceof ListHolder) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                ((ListHolder) holder).tvName.setText(datas.get(position-1).getName());
                ((ListHolder) holder).tvContent.setText(datas.get(position-1).getContent());
                ((ListHolder) holder).tvPublishTime.setText(datas.get(position-1).getDate().toString());
                Glide.with(context).load(datas.get(position-1).getHeadImgUrl()).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(42), ConvertUtils.dp2px(42)) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        ((ListHolder) holder).ivHeadImage.setImageBitmap(resource);
                    }
                });
                return;
            }
            return;
        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }else{
            return;
        }
    }



    @Override
    public int getItemCount() {
        if(mHeaderView == null && mFooterView == null){
            return datas.size();
        }else if(mHeaderView == null && mFooterView != null){
            return datas.size() + 1;
        }else if (mHeaderView != null && mFooterView == null){
            return datas.size() + 1;
        }else {
            return datas.size() + 2;
        }
    }



    class ListHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvContent;
        TextView tvPublishTime;
        ImageView ivHeadImage;

        public ListHolder(View view) {
            super(view);
            //如果是headerview或者是footerview,直接返回
            if (view == mHeaderView){
                return;
            }
            if (view == mFooterView){
                return;
            }
            tvName = (TextView) view.findViewById(R.id.tv_comment_name);
            tvContent = (TextView) view.findViewById(R.id.tv_comment_content);
            tvPublishTime = (TextView) view.findViewById(R.id.tv_comment_publishTime);
            ivHeadImage = (ImageView) view.findViewById(R.id.civ_comment_headImg);
        }
    }
}
